package com.lanjiu.lib.business.util;

import com.lanjiu.pro.file.FriendsTransmissionRequest;
import com.lanjiu.pro.file.FriendsTransmissionResponse;
import com.lanjiu.pro.file.GroupTransmissionRequest;
import com.lanjiu.pro.file.GroupTransmissionResponse;
import com.lanjiu.pro.file.Head;
import com.lanjiu.pro.file.OriginalPictureVideoFriendsTransmission;
import com.lanjiu.pro.file.OriginalPictureVideoGroupTransmission;
import com.lanjiu.lib.business.TransmissionConstant;

import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

public class FileJCRC32 {
    /**
     * 编码
     *
     * @param data
     * @return
     */
    public static long encode(String data) {
        CRC32 crc32 = new CRC32();
        crc32.update(data.getBytes(StandardCharsets.UTF_8));
        return crc32.getValue();
    }

    //给请求数据包加校验信息
    public FriendsTransmissionRequest packageCheckSumRequest(OriginalPictureVideoFriendsTransmission msg) {

        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoFriendsTransmission(msg);
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));
        FriendsTransmissionRequest friendsTransmissionRequest = FriendsTransmissionRequest.newBuilder()
                .setFriendsPictureVideoMessage(msg)
                .setChecksum(checksum_Translate).build();
        return friendsTransmissionRequest;
    }

    //给响应数据包加校验信息
    public FriendsTransmissionResponse packageCheckSumResponse(OriginalPictureVideoFriendsTransmission msg) {
        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoFriendsTransmission(msg);
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));
        FriendsTransmissionResponse friendsTransmissionRequest = FriendsTransmissionResponse.newBuilder()
                .setFriendsPictureVideoMessage(msg)
                .setChecksum(checksum_Translate).build();
        return friendsTransmissionRequest;
    }

    //给请求数据包加校验信息
    public GroupTransmissionRequest packageCheckSumGroupRequest(OriginalPictureVideoGroupTransmission msg) {

        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoGroupTransmission(msg);
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));
        GroupTransmissionRequest groupTransmissionRequest = GroupTransmissionRequest.newBuilder()
                .setGroupPictureVideoMessage(msg)
                .setChecksum(checksum_Translate).build();
        return groupTransmissionRequest;
    }

    //给响应数据包加校验信息
    public GroupTransmissionResponse packageCheckSumGroupResponse(OriginalPictureVideoGroupTransmission msg) {

        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoGroupTransmission(msg);
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));
        GroupTransmissionResponse groupTransmissionResponse = GroupTransmissionResponse.newBuilder()
                .setGroupPictureVideoMessage(msg)
                .setChecksum(checksum_Translate).build();
        return groupTransmissionResponse;
    }


    //checksum的自定义校验函数
    public FilePackageCheckSumResult checkSumValidateGroupResponse(GroupTransmissionResponse groupTransmissionResponse) {
        String checksum = groupTransmissionResponse.getChecksum();
        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoGroupTransmission(groupTransmissionResponse.getGroupPictureVideoMessage());
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));

        if (checksum_Translate.equals(checksum)) {
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(true, groupTransmissionResponse);
            return checkSumResult;
        } else {
            GroupTransmissionResponse checkUnified = GroupTransmissionResponse.newBuilder()
                    .setGroupPictureVideoMessage(OriginalPictureVideoGroupTransmission
                            .newBuilder()
                            .setHead(Head.newBuilder()
                                    .setToken(TransmissionConstant.CONSTANT_TOKEN)
                                    .setStatusReport(TransmissionConstant.CRC_FAILURE)
                                    .build())
                            .build())
                    .setChecksum(checksum_Translate)
                    .build();
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(false, checkUnified);
            return checkSumResult;
        }
    }

    //checksum的自定义校验函数
    public FilePackageCheckSumResult checkSumValidateGroupRequest(GroupTransmissionRequest groupTransmissionResponse) {
        String checksum = groupTransmissionResponse.getChecksum();
        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoGroupTransmission(groupTransmissionResponse.getGroupPictureVideoMessage());
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));

        if (checksum_Translate.equals(checksum)) {
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(true, groupTransmissionResponse);
            return checkSumResult;
        } else {
            GroupTransmissionRequest checkUnified = GroupTransmissionRequest.newBuilder()
                    .setGroupPictureVideoMessage(OriginalPictureVideoGroupTransmission
                            .newBuilder()
                            .setHead(Head.newBuilder()
                                    .setToken(TransmissionConstant.CONSTANT_TOKEN)
                                    .setStatusReport(TransmissionConstant.CRC_FAILURE)
                                    .build())
                            .build())
                    .setChecksum(checksum_Translate)
                    .build();
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(false, checkUnified);
            return checkSumResult;
        }
    }

    //checksum的自定义校验函数
    public FilePackageCheckSumResult checkSumValidateFriendsResponse(FriendsTransmissionResponse groupTransmissionResponse) {
        String checksum = groupTransmissionResponse.getChecksum();
        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoFriendsTransmission(groupTransmissionResponse.getFriendsPictureVideoMessage());
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));

        if (checksum_Translate.equals(checksum)) {
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(true, groupTransmissionResponse);
            return checkSumResult;
        } else {
            FriendsTransmissionRequest checkUnified = FriendsTransmissionRequest.newBuilder()
                    .setFriendsPictureVideoMessage(OriginalPictureVideoFriendsTransmission
                            .newBuilder()
                            .setHead(Head.newBuilder()
                                    .setToken(TransmissionConstant.CONSTANT_TOKEN)
                                    .setStatusReport(TransmissionConstant.CRC_FAILURE)
                                    .build())
                            .build())
                    .setChecksum(checksum_Translate)
                    .build();
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(false, checkUnified);
            return checkSumResult;
        }
    }

    //checksum的自定义校验函数
    public FilePackageCheckSumResult checkSumValidateFriendsRequest(FriendsTransmissionRequest groupTransmissionResponse) {
        String checksum = groupTransmissionResponse.getChecksum();
        CustomGRPCEntityToString customGRPCEntityToString = new CustomGRPCEntityToString();
        String protocolHexString = customGRPCEntityToString.toStringOriginalPictureVideoFriendsTransmission(groupTransmissionResponse.getFriendsPictureVideoMessage());
        String checksum_Translate = String.valueOf(FileJCRC32.encode(protocolHexString));

        if (checksum_Translate.equals(checksum)) {
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(true, groupTransmissionResponse);
            return checkSumResult;
        } else {
            FriendsTransmissionResponse checkUnified = FriendsTransmissionResponse.newBuilder()
                    .setFriendsPictureVideoMessage(OriginalPictureVideoFriendsTransmission
                            .newBuilder()
                            .setHead(Head.newBuilder()
                                    .setToken(TransmissionConstant.CONSTANT_TOKEN)
                                    .setStatusReport(TransmissionConstant.CRC_FAILURE)
                                    .build())
                            .build())
                    .setChecksum(checksum_Translate)
                    .build();
            FilePackageCheckSumResult checkSumResult = new FilePackageCheckSumResult(false, checkUnified);
            return checkSumResult;
        }
    }
}
